<template>
  <div class="app-container">
    <SearchForm
      v-show="showSearch"
      v-model="queryParams"
      :options="options.formOps"
      @onQuery="getList"
    >
    </SearchForm>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['preventionDevice:create']"
          >新增</el-button
        >
      </el-col>

      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:user:remove']"
          >删除</el-button
        >
      </el-col> -->

      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:user:export']"
          >导出</el-button
        >
      </el-col> -->
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <Table
      ref="table"
      :auto-featch="true"
      :fetch-data="fetchData"
      :options="options.table"
    >
      <template #default>
        <el-table-column
          label="责任部门"
          align="center"
          key="deptName"
          prop="deptName"
        />
        <el-table-column
          label="分析单元个数"
          align="center"
          key="unitCount"
          prop="unitCount"
          width="120"
        />
      </template>
      <template #actionButton="{ row }">
        <el-button
          type="text"
          icon="Edit"
          @click="handleEdit(row)"
          v-hasPermi="['preventionDevice:update']"
          >修改</el-button
        >

        <el-button
          type="text"
          icon="Delete"
          style="color: red"
          @click="handleDel(row)"
          v-hasPermi="['preventionDevice:delete']"
          >删除</el-button
        >
      </template>
      <template #dangerNames="{ row }">
        {{ row.dangerName }}
      </template>
    </Table>
    <DialogVform
      :formJson="formJson"
      ref="dialogVform"
      @onSubmit="onSubmit"
      :slots="['deptSlot']"
      :title="dialogType == 'add' ? '新增装置单元' : '修改装置单元'"
    >
      <template #deptSlot>
        <el-form-item label="责任部门" prop="deptId" label-position="right">
          <TreeSelect
            v-model="form.deptId"
            :options="deptTree"
            @change="changeDept"
          ></TreeSelect>
        </el-form-item>
      </template>
    </DialogVform>
  </div>
</template>

<script setup>
import Table from "@/components/Table/index.vue";
import SearchForm from "@/components/searchForm/index";
import DialogVform from "@/components/dialogVform/index";
import { apis as APIS } from "@/api/tool/zzgl.js";
import formJson from "./components/options.json";
import { useAsyncData, useFormJson } from "@/views/hooks/useAsyncData.js";
import { deptTreeSelect } from "@/api/system/user";
import TreeSelect from "@/components/TreeSelect";
const { proxy } = getCurrentInstance();

const ops = useFormJson(formJson, {
  sort: [],
  sortable: [],
});
const options = reactive({
  table: ops,
  formOps: ops.filter((i) =>
    ["deviceName", "dangerName", "dangerLevel"].includes(i.prop)
  ),
});
const showSearch = ref(true);
const dialogVform = ref();
const table = ref();
const changeDept = (val, label) => {
  form.value.deptName = label;
  form.value.deptId = val;
};
const plugEdit = (row) => {
  form.value.deptName = row.deptName;
  form.value.deptId = row.deptId;
  return { ...row, dangerNames: row.dangerName.split(",") };
};
const plugSubmit = (data) => {
  form.value.dangerName = data.dangerNames.join(",");
};
const {
  onSubmit,
  fetchData,
  handleAdd,
  handleEdit,
  getList,
  handleDel,
  queryParams,
  dialogType,
  form,
} = useAsyncData({
  APIS,
  formJson,
  dialogVform,
  table,
  proxy,
  plugEdit,
  plugSubmit,
});
const deptTree = ref([]);
onMounted(() => {
  deptTreeSelect().then((res) => {
    deptTree.value = res.data;
  });
});
</script>

<style lang="scss" scoped></style>
